/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Window.java
 *
 * Created on 22/08/2012, 12:34:16 AM
 */
package app.scraper.slug;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Luis Javier
 */
public class Window extends javax.swing.JFrame {

    List<Integer> list=new ArrayList<Integer>();
    
    /** Creates new form Window */
    public Window() {
        initComponents();
        this.setLocation(350, 300);
        this.setSize(650, 630);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chooser = new javax.swing.JFileChooser();
        jLabel1 = new javax.swing.JLabel();
        button_loadpage = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        combo_schools = new javax.swing.JComboBox();
        button_select_schools = new javax.swing.JButton();
        bar = new javax.swing.JProgressBar();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        table_departments = new javax.swing.JTable();
        button_delete_all = new javax.swing.JButton();
        button_select_all = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        box_file = new javax.swing.JTextField();
        button_select = new javax.swing.JButton();
        button_scrape = new javax.swing.JButton();
        bar2 = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SlugBooks Scraper");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Slug Books Scraper");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 30, -1, -1));

        button_loadpage.setText("Load Page");
        button_loadpage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_loadpageActionPerformed(evt);
            }
        });
        getContentPane().add(button_loadpage, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 60, -1, -1));

        jLabel2.setText("Select School:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 120, -1, -1));

        getContentPane().add(combo_schools, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 120, 270, -1));

        button_select_schools.setText("Ok");
        button_select_schools.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_select_schoolsActionPerformed(evt);
            }
        });
        getContentPane().add(button_select_schools, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 120, -1, -1));
        getContentPane().add(bar, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 64, -1, 20));

        jLabel3.setText("Departments");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 180, -1, -1));

        table_departments.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Select", "Department"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(table_departments);
        table_departments.getColumnModel().getColumn(0).setPreferredWidth(50);
        table_departments.getColumnModel().getColumn(0).setMaxWidth(50);

        getContentPane().add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 200, 400, 270));

        button_delete_all.setText("Delete All");
        button_delete_all.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_delete_allActionPerformed(evt);
            }
        });
        getContentPane().add(button_delete_all, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 170, -1, -1));

        button_select_all.setText("Select All");
        button_select_all.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_select_allActionPerformed(evt);
            }
        });
        getContentPane().add(button_select_all, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 170, -1, -1));

        jLabel4.setText("Select OutputFile:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 480, -1, -1));
        getContentPane().add(box_file, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 480, 290, -1));

        button_select.setText("Select");
        button_select.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_selectActionPerformed(evt);
            }
        });
        getContentPane().add(button_select, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 480, -1, -1));

        button_scrape.setText("Scrape");
        button_scrape.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_scrapeActionPerformed(evt);
            }
        });
        getContentPane().add(button_scrape, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 510, -1, -1));
        getContentPane().add(bar2, new org.netbeans.lib.awtextra.AbsoluteConstraints(186, 564, 210, 20));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    Scraper s=null;

    private void button_loadpageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_loadpageActionPerformed
        // TODO add your handling code here:
        combo_schools.removeAllItems();
       s=new Scraper();
       s.school=combo_schools;
       s.bar=bar;
       s.start();
    }//GEN-LAST:event_button_loadpageActionPerformed

    private void button_select_schoolsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_select_schoolsActionPerformed
        // TODO add your handling code here:
        int index=combo_schools.getSelectedIndex();
        if(s!=null){           
            DefaultTableModel model=(DefaultTableModel) table_departments.getModel();
            model.setRowCount(0);
            s.depart=table_departments;
           s.loadDeparment(index);
            
            
           list.clear();
        }
    }//GEN-LAST:event_button_select_schoolsActionPerformed

    private void button_delete_allActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_delete_allActionPerformed
        // TODO add your handling code here:
         DefaultTableModel model=(DefaultTableModel) table_departments.getModel();
          for(int i=0; i<model.getRowCount(); i++){
            model.setValueAt(false, i, 0);
          
          }
        list.clear();
    }//GEN-LAST:event_button_delete_allActionPerformed

    private void button_select_allActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_select_allActionPerformed
        // TODO add your handling code here:
          DefaultTableModel model=(DefaultTableModel) table_departments.getModel();
          for(int i=0; i<model.getRowCount(); i++){
            model.setValueAt(true, i, 0);
          
          }
          
    }//GEN-LAST:event_button_select_allActionPerformed

    private void button_selectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_selectActionPerformed
        // TODO add your handling code here:
        chooser.showSaveDialog(this);
        File f=chooser.getSelectedFile();
        if(f!=null)
            box_file.setText(f.getAbsolutePath());
    }//GEN-LAST:event_button_selectActionPerformed
//scrape button
    private void button_scrapeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_scrapeActionPerformed
        // TODO add your handling code here:
        String file=box_file.getText();
        if (file != null&&!file.isEmpty()) {
            DefaultTableModel model = (DefaultTableModel) table_departments.getModel();
            for (int i = 0; i < table_departments.getRowCount(); i++) {
                if (table_departments.getValueAt(i, 0) != null) {
                    list.add(i);
                }
                //System.out.println(table_departments.getValueAt(i, 0));

            }
            s.bar2 = bar2;
            s.selection = list;
            s.file_name=file;
            s.getCourses();
        }else{
          JOptionPane.showMessageDialog(null, "Selected a file");
        
        }
    }//GEN-LAST:event_button_scrapeActionPerformed

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar bar;
    private javax.swing.JProgressBar bar2;
    private javax.swing.JTextField box_file;
    private javax.swing.JButton button_delete_all;
    private javax.swing.JButton button_loadpage;
    private javax.swing.JButton button_scrape;
    private javax.swing.JButton button_select;
    private javax.swing.JButton button_select_all;
    private javax.swing.JButton button_select_schools;
    private javax.swing.JFileChooser chooser;
    private javax.swing.JComboBox combo_schools;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable table_departments;
    // End of variables declaration//GEN-END:variables
}
